package com.lq.product.controller;

import com.lq.common.result.Result;
import com.lq.product.entity.Category;
import com.lq.product.service.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品分类表 前端控制器
 * </p>
 *
 * @author lq
 * @since 2025-08-23
 */
@RestController
@RequestMapping("/categories")
@Api(tags = "商品分类管理", description = "商品分类相关接口")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    @ApiOperation(value = "创建分类", notes = "创建一个新的商品分类")
    @PostMapping("/")
    public Result createCategory(@ApiParam(value = "分类信息", required = true) @RequestBody Category category) {
        categoryService.save(category);
        return Result.success(category);
    }

    @ApiOperation(value = "根据ID获取分类", notes = "根据分类ID获取分类详细信息")
    @GetMapping("/{id}")
    public Result getCategoryById(@ApiParam(value = "分类ID", required = true) @PathVariable Long id) {
        return Result.success(categoryService.getById(id));
    }

    @ApiOperation(value = "获取所有分类", notes = "获取系统中所有的商品分类")
    @GetMapping("/")
    public Result getAllCategories() {
        return Result.success(categoryService.list());
    }

    @ApiOperation(value = "获取分类树结构", notes = "获取商品分类的树形结构")
    @GetMapping("/tree")
    public Result getCategoryTree() {
        List<Category> categories = categoryService.getCategoryTree();
        return Result.success(categories);
    }


    @ApiOperation(value = "更新分类", notes = "根据分类ID更新分类信息")
    @PutMapping("/{id}")
    public Result updateCategory(@ApiParam(value = "分类ID", required = true) @PathVariable Long id,
                                 @ApiParam(value = "分类信息", required = true) @RequestBody Category category) {
        category.setId(id);
        categoryService.updateById(category);
        return Result.success(category);
    }

    @ApiOperation(value = "删除分类", notes = "根据分类ID删除分类")
    @DeleteMapping("/{id}")
    public Result deleteCategory(@ApiParam(value = "分类ID", required = true) @PathVariable Long id) {
        categoryService.removeById(id);
        return Result.success();
    }
}